package com.fenye.testflow.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fenye.testflow.model.common.Result;
import com.fenye.testflow.model.common.ResultCodeEnum;
import com.fenye.testflow.model.dto.TestRequestDto;
import com.fenye.testflow.model.entity.testflow.TestRequest;
import com.fenye.testflow.service.TestRequestService;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/testRequest")
public class TestRequestController {
    @Autowired
    private TestRequestService testRequestService;
    @PostMapping("save")
    public Result save(@RequestBody TestRequest testRequest) {
        boolean isSave = testRequestService.save(testRequest);
        if (isSave) {
            return Result.build(null, ResultCodeEnum.SUCCESS);
        }
        return Result.build(null,ResultCodeEnum.DATA_ERROR);
    }

    @PostMapping("update")
    public Result update(@RequestBody TestRequest testRequest) {
        boolean isUpdate = testRequestService.updateById(testRequest);
        if (isUpdate) {
            return Result.build(null, ResultCodeEnum.SUCCESS);
        }
        return Result.build(null,ResultCodeEnum.DATA_ERROR);
    }

    @DeleteMapping("/delete/{testRequestId}")
    public Result delete(@PathVariable Long testRequestId) {
        boolean isDelete = testRequestService.removeById(testRequestId);
        if (isDelete) {
            return Result.build(null, ResultCodeEnum.SUCCESS);
        }
        return Result.build(null,ResultCodeEnum.DATA_ERROR);
    }

    @GetMapping("/findById/{testRequestId}")
    public Result findByid(@RequestParam Long testRequestId) {
        TestRequest testRequest = testRequestService.getById(testRequestId);
        if (testRequest == null) {
            return Result.build(null,ResultCodeEnum.DATA_ERROR);
        }
        return Result.build(testRequest, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("findByPage/{page}/{size}")
    public Result findByPage(TestRequest testRequest,
                             @PathVariable int page,
                             @PathVariable int size) {
        LambdaQueryWrapper<TestRequest> queryWrapper = new LambdaQueryWrapper<>();

        if (testRequest.getProjectId()!= null){
            queryWrapper.eq(TestRequest::getProjectId,testRequest.getProjectId());
        }
        if (testRequest.getVersionNumber() != null){
            queryWrapper.eq(TestRequest::getVersionNumber,testRequest.getVersionNumber());
        }
        Page<TestRequest> pageParam = new Page<>(page, size);
        Page<TestRequest> pageData = testRequestService.page(pageParam, queryWrapper);
        return Result.build(pageData, ResultCodeEnum.SUCCESS);
    }


    /**
     * 更新提测单状态
     */

    @PostMapping("updateTestRequestStatusById")
    public Result updateTestRequestStatusById(@RequestBody TestRequestDto testRequestDto) {
        //处理提测动作，填充提测时间
      Boolean isUpdate =  testRequestService.updateTestRequestStatusById(testRequestDto);
      return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @GetMapping("/exportData")
    public void exportData( HttpServletResponse responseTestRequest ,TestRequest testRequest) {
        System.out.println("11111111");
       Boolean isExport =  testRequestService.exportData(testRequest,responseTestRequest);
    }

    @GetMapping("queryTodo/{page}/{size}")
    public Result queryTodo(@PathVariable long page, @PathVariable int size,TestRequest testRequest) {
        Page<TestRequest> pageParam = new Page<>(page, size);
        Page<TestRequest> pageData  =  testRequestService.queryTodoTestRequest(pageParam);
        return Result.build(pageData, ResultCodeEnum.SUCCESS);
    }
}
